package com.icode.service.impl.house;

import com.icode.core.dto.house.MyHouseDTO;
import com.icode.core.model.house.Assignment;
import com.icode.core.model.house.MyHouse;
import com.icode.core.model.house.MyHouseStatus;
import com.icode.core.model.house.Rent;
import com.icode.core.model.user.User;
import com.icode.core.model.user.UserInfo;
import com.icode.core.shared.MyHouseHandle;
import com.icode.dao.AssignmentDao;
import com.icode.dao.MyHouseDao;
import com.icode.dao.RentDao;
import com.icode.service.house.MyHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-9-16
 * Time: 上午10:41
 * To change this template use File | Settings | File Templates.
 */
@Service("myHouseService")
@Transactional
public class MyHouseServiceImpl implements MyHouseService {

    @Autowired
    private MyHouseDao myHouseDao;
    @Autowired
    private AssignmentDao assignmentDao;
    @Autowired
    private RentDao rentDao;

    @Override
    @Transactional
    public List<MyHouseDTO> findMyHouse(User user) {
        UserInfo userInfo = myHouseDao.findUserInfoByUser(user);
        List<MyHouse> myHouse = myHouseDao.findMyHouse(userInfo);
        List<MyHouseDTO> myHouseDTOs = new ArrayList<>();
        if (myHouse != null) {
            myHouseDTOs = MyHouseDTO.toDTOs(myHouse);
        }
        return myHouseDTOs;
    }

    @Override
    @Transactional
    public List<MyHouseDTO> findMyChangeHouse(User user) {
        UserInfo userInfo = myHouseDao.findUserInfoByUser(user);
        List<MyHouse> myHouses = myHouseDao.findMyChangeHouse(userInfo);
        List<MyHouseDTO> myHouseDTOs = new ArrayList<>();
        if (myHouses != null) {
            myHouseDTOs = MyHouseDTO.toDTOs(myHouses);
        }
        return myHouseDTOs;
    }

    @Override
    @Transactional
    public List<MyHouseDTO> findMyTransferHouses(User user) {
        UserInfo userInfo = myHouseDao.findUserInfoByUser(user);
        List<MyHouse> myHouses = myHouseDao.findMyTransferHouses(userInfo);
        List<MyHouseDTO> myHouseDTOs = new ArrayList<>();
        if (myHouses != null) {
            myHouseDTOs = MyHouseDTO.toDTOs(myHouses);
        }
        return myHouseDTOs;
    }

    @Override
    @Transactional
    public MyHouseDTO findMyHouseByGuid(String myHouseByGuid) {
        MyHouse myHouse = myHouseDao.findByGuid(MyHouse.class, myHouseByGuid);
        return MyHouseDTO.toDTO(myHouse);
    }

    @Override
    @Transactional
    public void initRent(String myHouseGuid, String price, String days, String description) {
        MyHouse myHouse = myHouseDao.findByGuid(MyHouse.class, myHouseGuid);
        myHouse.updateStatus(MyHouseStatus.TORENT);
        Rent rent = new Rent(myHouse, price, days, description, null, null);
        myHouseDao.saveOrUpdate(rent);
    }

    @Override
    @Transactional
    public String changeMyHouseStatus(String rentGuid) {
        Rent rent = myHouseDao.findByGuid(Rent.class, rentGuid);
        MyHouse myHouse = rent.myHouse();
        myHouse.updateStatus(MyHouseStatus.RENTING);
        return myHouse.guid();
    }

    @Override
    @Transactional
    public void myHouserToAssigmnent(String myHouseGuid) {
        MyHouse myHouse = myHouseDao.findByGuid(MyHouse.class, myHouseGuid);
        Rent rent = rentDao.findRentByMyHouse(myHouse);
        if (null == rent) {
            if (assignmentDao.getAssignmentsByMyHouse(myHouse) == null && myHouse.myHouseStatus().equals(MyHouseStatus.ACTIVITY)) {
                Assignment assignment = new Assignment(myHouse);
                assignment.updateStatus(MyHouseStatus.TOASSIGNMNET);
                assignmentDao.saveOrUpdate(assignment);
                myHouse.updateStatus(MyHouseStatus.TOASSIGNMNET);
            }
        }
    }

    @Override
    @Transactional
    public List<MyHouseDTO> findMyHouseisToAssignmnet() {
        List<MyHouse> myHouses = myHouseDao.findMyHouseisToAssignmnet();
        List<MyHouseDTO> myHouseDTOs = new ArrayList<>();
        if (myHouses != null) {
            myHouseDTOs = MyHouseDTO.toDTOs(myHouses);
        }
        return myHouseDTOs;
    }

    @Override
    public List<MyHouseDTO> findMyHouseByCondition(String area, String week, String theme) {
        area = MyHouseHandle.initAreaHql(area);
        theme = MyHouseHandle.initThemeHql(theme);
        List<MyHouse> myHouses = myHouseDao.findMyHouseByCondition(area, week, theme);
        List<MyHouseDTO> myHouseDTOs = new ArrayList<>();
        if (myHouses != null) {
            myHouseDTOs = MyHouseDTO.toDTOs(myHouses);
        }
        return myHouseDTOs;
    }

    @Override
    public List<MyHouseDTO> findMyHouseByUsername(String username) {
        List<MyHouse> myHouses = myHouseDao.findMyHouseByUsername(username);
        if (myHouses == null) {
            return null;
        } else {
            List<MyHouseDTO> myHouseDTOs = MyHouseDTO.toDTOs(myHouses);
            return myHouseDTOs;
        }
    }

    @Override
    public List<MyHouseDTO> findMyExChangeHouseByUsername(String username) {
        List<MyHouse> myHouses = myHouseDao.findMyExChangeHouseByUsername(username, MyHouseStatus.CHANGEHOUSE, true);
        List<MyHouseDTO> myHouseDTOs = new ArrayList<>();
        if (username != null) {
            myHouseDTOs = MyHouseDTO.toDTOs(myHouses);
        }
        return myHouseDTOs;
    }

    @Override
    public List<MyHouseDTO> findMyExChangingHouseByUsername(String username) {
        List<MyHouse> myHouses = myHouseDao.findMyExChangeHouseByUsername(username, MyHouseStatus.CHANGING, false);
        List<MyHouseDTO> myHouseDTOs = new ArrayList<>();
        if (username != null) {
            myHouseDTOs = MyHouseDTO.toDTOs(myHouses);
            myHouseDTOs = MyHouseDTO.toDTOs(myHouses);
        }
        return myHouseDTOs;
    }
}
